<template>
  <div>
    <!-- 库房管理 -->
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-form :inline="true" :model="queryParam" class="demo-form-inline" size="medium">
        <el-form-item label="库房名称">
          <el-input v-model="queryParam.repositoryName" placeholder="请输入库房名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="rest">重置</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-table :data="tableData" border style="width: 100%" height="50vh">
        <el-table-column type="index" width="50" label="序号" align="center"> </el-table-column>
        <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
          :min-width="item.width" show-overflow-tooltip>
          <template #default="scope">
            <div v-if="item.prop === 'disabledFlag'">
              <el-tag v-show="scope.row.disabledFlag == 0" type="success">正常</el-tag>
              <el-tag v-show="scope.row.disabledFlag == 1" type="danger">禁用</el-tag>
            </div>
            <div v-else>{{ scope.row[item.prop] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="">
          <template slot-scope="{ row }">
            <el-button icon="el-icon-edit" link size="mini" @click="handleEdit(row)">编辑</el-button>
            <el-popconfirm class="button10Left" title="确定要删除吗？" @confirm="() => handleDelete(row)">
              <el-button slot="reference" icon="el-icon-delete" link size="mini" class="danger_button">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <pagination ref="pagination" :totalCount="totalCount" @pagination="pagination"></pagination>
    </el-card>
    <repositoryDialog ref="repositoryDialog" @ok="loadData"></repositoryDialog>
  </div>
</template>

<script>
import { del, pageList } from '@/api/bas/repository'
import repositoryDialog from './repositoryDialog.vue'
export default {
  name: 'RFIDRepository',
  components: { repositoryDialog },
  data() {
    return {
      totalCount: 0, //总数
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam: {},
      tableData: [],
      tableProps: [
        { title: '库房名称', prop: 'repositoryName', width: '', checked: true },
        { title: '面积(㎡)', prop: 'repositoryArea', width: '', checked: true },
        { title: '地址', prop: 'repositoryAddress', width: '150', checked: true },
        { title: '备注', prop: 'note', width: '', checked: true },
      ],
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    onSubmit() {
      this.$refs.pagination.resetPagination()
    },
    rest() {
      this.queryParam = {}
      this.$refs.pagination.resetPagination()
    },
    loadData() {
      var param = Object.assign(this.queryParam, this.searchModel)
      pageList(param)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records
            this.totalCount = res.data.data.total
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    pagination(val) {
      this.searchModel.pageNo = val.page
      this.searchModel.pageSize = val.limit
      this.loadData()
    },
    doSearch(value) {
      this.queryParam = this.ArraryToObject(value)
      this.loadData()
    },
    // 打开对话框
    handleAdd() {
      this.$refs.repositoryDialog.add()
    },
    handleEdit(row) {
      this.$refs.repositoryDialog.edit(row)
    },
    handleDelete(row) {
      del(row.repositoryId)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.loadData()
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
      // this.tableData = this.tableData.filter(it => it.id !== row.id)
    },
  },
}
</script>

<style scoped lang="less">
.demo-form-inline {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
